﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using CodeFirstDemo.Model;

namespace CodeFirstDemo.Data
{
    public class CodeFirstDBInitializer : DropCreateDatabaseIfModelChanges<CodeFirstDBContext>
    {
        protected override void Seed(CodeFirstDBContext context)
        {
            //base.Seed(context);
            var users = new List<User>
            {
                //new User { Name = "Carson",  Address =  new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                //new User { Name = "Meredith", Address = new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                //new User { Name = "Arturo",  Address = new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                //new User { Name = "Gytis",  Address =   new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                //new User { Name = "Yan",    Address =  new Address { City = "Alexander", Street="sdd", ZipCode="11111" } }

                new User { Name = "Carson",  BillingAddress =  new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                new User { Name = "Meredith", BillingAddress = new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                new User { Name = "Arturo",  BillingAddress = new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                new User { Name = "Gytis",  BillingAddress =   new Address { City = "Alexander", Street="sdd", ZipCode="11111" } },
                new User { Name = "Yan",    BillingAddress =  new Address { City = "Alexander", Street="sdd", ZipCode="11111" } }


            };
            users.ForEach(s => context.Users.Add(s));
            context.SaveChanges();

            var categories = new List<Category>
            {
                new Category { Name = "Carson",  CategoryCode="aaa", Products = new List<Product>() },
                new Category { Name = "Meredith", CategoryCode="bbb" , Products = new List<Product>()},
                new Category { Name = "Arturo",  CategoryCode="ccc" , Products = new List<Product>()},
                new Category { Name = "Gytis",   CategoryCode="ddd" , Products = new List<Product>()},
                new Category { Name = "Yan",   CategoryCode="eee" , Products = new List<Product>() }
            };
            categories.ForEach(s => context.Categories.Add(s));
            context.SaveChanges();

            var products = new List<Product>
            {
                //new Product { Name = "Carson",   PrimaryCategoryCode = categories[0].CategoryCode, PrimaryCategory = new Category()  },
                //new Product { Name = "Meredith", PrimaryCategoryCode = categories[0].CategoryCode, PrimaryCategory = new Category()  },
                //new Product { Name = "Arturo"  , PrimaryCategoryCode = categories[1].CategoryCode, PrimaryCategory = new Category() },
                //new Product { Name = "Gytis"  ,  PrimaryCategoryCode = categories[2].CategoryCode, PrimaryCategory = new Category() },
                //new Product { Name = "Yan",      PrimaryCategoryCode = categories[3].CategoryCode, PrimaryCategory = new Category() }

                new Product { Name = "Carson",   PrimaryCategoryCode = categories[0].CategoryCode, PrimaryCategory = categories[0]  },
                new Product { Name = "Meredith", PrimaryCategoryCode = categories[0].CategoryCode, PrimaryCategory = categories[0]  },
                new Product { Name = "Arturo"  , PrimaryCategoryCode = categories[1].CategoryCode, PrimaryCategory = categories[1] },
                new Product { Name = "Gytis"  ,  PrimaryCategoryCode = categories[2].CategoryCode, PrimaryCategory = categories[2] },
                new Product { Name = "Yan",      PrimaryCategoryCode = categories[3].CategoryCode, PrimaryCategory = categories[3] }



                //new Product { Name = "Carson", PrimaryCategory = new Category()  },
                //new Product { Name = "Meredith", PrimaryCategory = new Category()  },
                //new Product { Name = "Arturo"  , PrimaryCategory = new Category() },
                //new Product { Name = "Gytis"  ,  PrimaryCategory = new Category() },
                //new Product { Name = "Yan",  PrimaryCategory = new Category() }

                //new Product { Name = "Carson"  },
                //new Product { Name = "Meredith" },
                //new Product { Name = "Arturo"  },
                //new Product { Name = "Gytis"  },
                //new Product { Name = "Yan" }
            };
            products.ForEach(s => context.Products.Add(s));

            //try
            //{
            //    context.SaveChanges();// 'EntityValidationErrors' 
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}


            //products[0].PrimaryCategory.CategoryCode = categories[0].CategoryCode;
            //products[1].PrimaryCategory.CategoryCode = categories[0].CategoryCode;
            //products[2].PrimaryCategory.CategoryCode = categories[1].CategoryCode;
            //products[3].PrimaryCategory.CategoryCode = categories[2].CategoryCode;
            //products[4].PrimaryCategory.CategoryCode = categories[3].CategoryCode;

            //products[0].PrimaryCategoryCode = categories[0].CategoryCode;
            //products[0].PrimaryCategoryCode = categories[0].CategoryCode;
            //products[0].PrimaryCategoryCode = categories[1].CategoryCode;
            //products[0].PrimaryCategoryCode = categories[2].CategoryCode;
            //products[0].PrimaryCategoryCode = categories[3].CategoryCode;

            //categories[0].Products.Add(products[0]);
            //categories[0].Products.Add(products[1]);
            //categories[1].Products.Add(products[2]);
            //categories[2].Products.Add(products[3]);
            //categories[3].Products.Add(products[4]);

            context.SaveChanges();

        }
    }
}
